package com.jie.deletefile.Histogram;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.jie.deletefile.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/29 0029.
 */
public class HistogramView extends View implements Runnable {

    private Paint paint;
    private int scaleTextSize;
    private int textSize;
    private int scaleTextWidth;
    private int textWidth;
    private int lineColor;//x,y轴颜色
    private int hColor;//柱形图颜色
    private int count;//圆柱个数
    private int sTextColor;//刻度颜色
    private int width;//整个view的高度
    private int textColor;
    private int height;
    private float mWidth;
    private float mHeight;
    private int hCount;
    private List<String> xScale;//x轴文字
    private List<String> yScale;
    private List<Integer> datas;//柱形图长度集合
    private float maxSize;//最大高度
    private boolean isOpenAnim;
    private String TAG = "HistogramView";
    private Handler handler = new Handler();
    private int drawHeight;
    private float cruHeight;
    private Path path;
    private boolean isShowData;


    public HistogramView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HistogramView(Context context) {
        super(context);
        init();
    }

    private void init() {
        lineColor = getResources().getColor(R.color.piechar_base);
        hColor = getResources().getColor(R.color.piechar_phone);
        sTextColor = getResources().getColor(R.color.piechar_sdcard);
        textColor = getResources().getColor(R.color.cardview_dark_background);
        count = 5;
        hCount = 5;
        paint = new Paint();
        xScale = new ArrayList<String>() {{
            add("1月");
            add("2月");
            add("3月");
            add("4月");
            add("5月");

        }};
        yScale = new ArrayList<String>() {{
            add("100");
            add("200");
            add("300");
            add("400");
            add("500");
        }};

        datas = new ArrayList<Integer>() {{
            add(100);
            add(300);
            add(400);
            add(150);
            add(250);
        }};
        maxSize = 500;
        isOpenAnim = true;
        path = new Path();
        isShowData = true;
        scaleTextSize = 20;
        scaleTextWidth = 3;
        textSize = 20;
        textWidth = 3;


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        if (mode == MeasureSpec.EXACTLY) {
            width = widthSize;
            height = heightSize;
            mWidth = width / (count + 1) / 3;
            mHeight = height / (count + 2);

        }

        setMeasuredDimension(width, height);
        drawHeight = 1;

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawLine(canvas);
        drawScale(canvas);
        drawHist(canvas);

    }

    /**
     * 画柱状图
     * @param canvas
     */
    private void drawHist(Canvas canvas) {

        handler.postDelayed(this, 1);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(hColor);
        paint.setStrokeWidth(2 * mWidth);
        for (int i = 0; i < datas.size(); i++) {

            cruHeight = (float) ((datas.get(i) / (maxSize / hCount) + 1)) * mHeight;
            //判断是否使用动画效果
            if (!isOpenAnim) {
                canvas.drawLine(((i + 1) * 3 + 1) * mWidth, height - mHeight, (i + 1) * 3 * mWidth, cruHeight, paint);
            } else {
                canvas.drawLine(((i + 1) * 3 + 1) * mWidth, height - mHeight, ((i + 1) * 3 + 1) * mWidth, height - mHeight - (height - mHeight - cruHeight) * drawHeight / 100, paint);
            }
            Paint paint1 = new Paint();
            paint1.setStrokeWidth(textWidth);
            paint1.setStyle(Paint.Style.STROKE);
            paint1.setTextSize(textSize);

            if (isShowData) {

                Log.e(TAG, "drawHist: draw"+drawHeight );
                if (drawHeight==100)
                    canvas.drawText(datas.get(i)+"",((i + 1) * 3 + 1) * mWidth,cruHeight,paint1);

            }
        }
    }

    /**
     * 画刻度参数
     *
     * @param canvas
     */
    private void drawScale(Canvas canvas) {
        paint.setColor(sTextColor);
        paint.setTextSize(scaleTextSize);
        paint.setStrokeWidth(scaleTextWidth);
        for (int i = 0; i < count; i++) {
            canvas.drawText(xScale.get(i), (mWidth * (i + 1) * 3 + mWidth), height - mHeight / 2, paint);
        }
        for (int i = 0; i < hCount; i++) {
            canvas.drawText(yScale.get(hCount - i - 1), 0, (i + 1) * mHeight, paint);
            canvas.drawLine(2 * mWidth - 5, (i + 1) * mHeight, 2 * mWidth + 5, (i + 1) * mHeight, paint);
        }
    }

    /**
     * 画x,y轴
     *
     * @param canvas
     */
    private void drawLine(Canvas canvas) {
        paint.setColor(lineColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        canvas.drawLine(2 * mWidth, height - mHeight, 2 * mWidth, mHeight / 2, paint);//y
        canvas.drawLine(2 * mWidth, height - mHeight, width - mWidth / 2, height - mHeight, paint);//x

        paint.setStyle(Paint.Style.FILL);
        //// TODO: 2016/11/29 0029 画三角箭头
        path.moveTo(width - mWidth / 2 - 20, height - mHeight - 10);
        path.lineTo(width - mWidth / 2 - 20, height - mHeight + 10);
        path.lineTo(width - mWidth / 2, height - mHeight);
        canvas.drawPath(path, paint);

        path.moveTo(2 * mWidth - 10, mHeight / 2 + 20);
        path.lineTo(2 * mWidth + 10, mHeight / 2 + 20);
        path.lineTo(2 * mWidth, mHeight / 2);
        canvas.drawPath(path, paint);

    }

    @Override
    public void run() {
        drawHeight += 1;
        if (drawHeight > 100) {
            return;
        } else {
            invalidate();
        }
    }

    /**
     * 设置x,y轴颜色
     * @param lineColor
     */
    public void setLineColor(int lineColor) {
        this.lineColor = lineColor;
    }

    /**
     * 设置柱状图颜色
     * @param hColor
     */
    public void sethColor(int hColor) {
        this.hColor = hColor;
    }

    /**
     * 设置刻度文字颜色
     * @param textColor
     */
    public void setScaleTextColor(int textColor) {
        this.sTextColor = textColor;
    }

    /**
     * 设置柱状图个数.
     * @param count
     */
    public void setCount(int count) {
        this.count = count;
    }

    /**
     * 设置x轴参数
     * @param xScale
     */
    public void setxScale(List<String> xScale) {
        this.xScale = xScale;
    }

    public void setyScale(List<String> yScale) {
        this.yScale = yScale;
    }

    /**
     * 设置柱状图参数集合
     * @param datas
     */
    public void setDatas(List<Integer> datas) {
        this.datas = datas;
    }

    /**
     * 设置柱状图最大值
     * @param maxSize
     */
    public void setMaxSize(float maxSize) {
        this.maxSize = maxSize;
    }

    /**
     * 设置是否开启柱状图动画
     * @param openAnim
     */
    public void setOpenAnim(boolean openAnim) {
        isOpenAnim = openAnim;
    }

    /**
     * 设置是否显示柱状图的具体参数
     * @param showData
     */
    public void setShowData(boolean showData) {
        isShowData = showData;
    }

    /**
     * 设置柱状图参数字体颜色
     * @param textColor
     */
    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }
}
